package net.ewon.avatar.agent;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import net.ewon.avatar.common.pkg.AvatarPackage;
import net.ewon.avatar.common.pkg.AvatarPackageFile;
import net.ewon.avatar.common.pkg.AvatarPackageWithFile;
import net.ewon.avatar.common.pkg.Version;

import org.apache.commons.io.FileUtils;
import org.apache.cxf.jaxrs.client.WebClient;

public class RepoTestUtils {

	public String fileName1;
	public String fileContent1;
	public String fileName2;
	public String fileContent2;
	public String batFileName;
	public String batFileContent;
	public String shFileName;
	public String shFileContent;

	private AvatarPackageRepo avatarPackageRepo;
	
	private WebClient pkgMgmtServiceClient;

	public RepoTestUtils(AvatarPackageRepo avatarPackageRepo, WebClient pkgMgmtServiceClient) {
		this.avatarPackageRepo = avatarPackageRepo;
		this.pkgMgmtServiceClient = pkgMgmtServiceClient;
	}

	public void installPackage(String pkgName, Version version) { 
		List<AvatarPackageFile> avatarPackageFiles = getMockedPackageFiles(pkgName, version);

		avatarPackageRepo.install(avatarPackageFiles);
	}
	
	public void uploadPackageToRemoteRepo(String pkgName, Version version) {
		pkgMgmtServiceClient.reset();
		pkgMgmtServiceClient.resetQuery();
		pkgMgmtServiceClient.path("AvatarPackageMgmtService/uploadAvatarPackageWithFiles");
		pkgMgmtServiceClient.type("application/json").accept("application/json");
		
		AvatarPackage avatarPackage = new AvatarPackage(pkgName, version, null);
		
		AvatarPackageWithFile pair =
				new AvatarPackageWithFile(avatarPackage,  getMockedPackageFiles(pkgName, version));

		String result = pkgMgmtServiceClient.post(pair, String.class);
	}
	
	public void removePackageFromRemoteRepo(String pkgName, Version version) {
		pkgMgmtServiceClient.reset();
		pkgMgmtServiceClient.type("application/json").accept("application/json");
		
		pkgMgmtServiceClient.path("AvatarPackageMgmtService/getAvatarPackage");
		pkgMgmtServiceClient.query("pkgName", pkgName);
		pkgMgmtServiceClient.query("version", version);
		
		AvatarPackage avatarPackage = pkgMgmtServiceClient.get(AvatarPackage.class);
		pkgMgmtServiceClient.reset();
		pkgMgmtServiceClient.path("AvatarPackageMgmtService/removeAvatarPackage");
		pkgMgmtServiceClient.type("application/json").accept("application/json");
		if(avatarPackage!=null) {		
			pkgMgmtServiceClient.post(avatarPackage);
		}
		
	}
	
	private List<AvatarPackageFile> getMockedPackageFiles(String pkgName, Version version) {
		fileName1 = pkgName + "/" + version + "/"
				+ "net/ewon/avatar/agent/test1.xml";
		fileContent1 = "<xml>xmlfiletest1</xml>";
		fileName2 = pkgName + "/" + version + "/"
				+ "test2.xml";
		fileContent2 = "<xml>xmlfiletest2</xml>";
		batFileName = pkgName + "/" + version + "/"
				+ "bin/startService.bat";
		batFileContent = "echo just a test";
		
		shFileName = pkgName + "/" + version + "/"
				+ "bin/startService.sh";
		shFileContent = "echo linux";
		

		List<AvatarPackageFile> avatarPackageFiles = new ArrayList<AvatarPackageFile>();
		avatarPackageFiles.add(new AvatarPackageFile(fileName1, fileContent1
				.getBytes()));
		avatarPackageFiles.add(new AvatarPackageFile(fileName2, fileContent2
				.getBytes()));
		avatarPackageFiles.add(new AvatarPackageFile(batFileName, batFileContent
				.getBytes()));
		avatarPackageFiles.add(new AvatarPackageFile(shFileName, shFileContent
				.getBytes()));
		return avatarPackageFiles;
	}

	public void removeRepo() {
		String baseLocation = avatarPackageRepo.getNormalizedBaseLocation();
		FileUtils.deleteQuietly(new File(baseLocation));
	}

	public void removeEnvironments(String root) {
		FileUtils.deleteQuietly(new File(root));
	}

}
